<?php

class Model_UserManager {
	
	/**
	 * 返回所有userGroup数据，不包括关联表数据
	 * $trad 是需要排除的树的根
	 * @return array
	 */
	function usergroupList($trad = null)
	{
		$tb_UserGroups =& FLEA::getSingleton('Table_UserGroups');
		/*@var $tb_UserGroups Table_UserGroups*/
		//设置不与任何关联表关联
		$tb_UserGroups->disableLinks();
		
		if($trad){
			//返回用户组树形结构
			$T = '├';
			$L = '└';
			$I = '│';
			$S = '　';
			$tree = $tb_UserGroups->findAll("user_group_id!={$trad}");
			$tree = array_to_tree($tree,'user_group_id');
			return dumpArrayTree($tree,0,$T,$L,$I,$S);
			
		}else{		
			//返回用户组树形结构
			$T = '<img src="images/join.gif" />';
			$L = '<img src="images/joinbottom.gif" />';
			$I = '<img src="images/line.gif" />';
			$S = '<img src="images/empty.gif" />';
			return dumpArrayTree(array_to_tree($tb_UserGroups->findAll(),'user_group_id'),0,$T,$L,$I,$S);
		}
	}
	
	/**
	 * 更新或者创建指定的userGroup条目
	 * @return boolin
	 */
	function saveUserGroup($thisUserGroup)
	{
		$tb_UserGroups =& FLEA::getSingleton('Table_UserGroups');
		/*@var $tb_UserGroups Table_UserGroups*/
		$tb_UserGroups->save($thisUserGroup);

	}
	
	/**
	 * 删除一条userGroup记录
	 * @return boolin
	 */
	function removeUserGroup($id)
	{
		
		$tb_UserGroups =& FLEA::getSingleton('Table_UserGroups');
		/*@var $tb_UserGroups Table_UserGroups*/
		$tb_Members =& FLEA::getSingleton('Table_Members');
		/*@var $tb_Members Table_Members*/
		
		if ($tb_UserGroups->findCount("parent_id={$id}") > 0) {
			//有子类
			return false;
		}else if($tb_Members->findCount("user_group_id={$id}") > 0) {
			//有用户
			return false;
		}else {
			return $tb_UserGroups->removeByPkv($id);
		}
		
	}
	
	/**
	 * 返回符合条件的user数据，不包括关联数据
	 * @return array
	 */
	function userList($ugid=null)
	{
		$tb_Members =& FLEA::getSingleton('Table_Members');
		/*@var $tb_Members Table_Members*/
		
		// 设置不获取任何关联数据
		$tb_Members->disableLinks();
		$tb_Members->enableLink('user_group');
		
		if ($ugid) {
			// 返回某个用户组的成员数据
			return $tb_Members->findAll("user_group_id={$ugid}");
		}else{
			// 返回全部成员数据
			return $tb_Members->findAll();
		}
		
	}
	
	/**
	 * 更新用户的基础数据，不包含关联数据的更新
	 * @return boolin
	 */
	function updateUser($userdata)
	{
		$tb_Members =& FLEA::getSingleton('Table_Members');
		/*@var $tb_Members Table_Members*/
		$tb_Members->clearLinks();
		
		// 更新密码
		if(isset($userdata['password'])){
			$tb_Members->updatePasswordById($userdata['member_id'],$userdata['password']);
		}
		
		// 获得更新前的用户信息
		$old_userdata = $tb_Members->find($userdata['member_id']);
		
		// 更新常规字段
		$isok = $tb_Members->update($userdata, false);
		
		// 如果用户组更换了
		if(isset($userdata['user_group_id']) && $userdata['user_group_id'] != $old_userdata['user_group_id']){
			$this->updateHas_members($old_userdata['user_group_id']);
			$this->updateHas_members($userdata['user_group_id']);
		}
		
		if($isok){
			return true;
		}else {
			return false;
		}
		
	}
	
	/**
	 * 创建新用户，不包含任何关联数据
	 * @return int primkey
	 */
	function createUser($userdata)
	{
		$tb_Members =& FLEA::getSingleton('Table_Members');
		/*@var $tb_Members Table_Members*/
		
		// 设置不获取任何关联数据
		$tb_Members->disableLinks();
		
		// 创建用户
		$member_id = $tb_Members->create($userdata);
		
		// 更新组成员数量
		$this->updateHas_members($userdata['user_group_id']);
		
		return $member_id;
	}
	
	/**
	 * 删除一条或多条用户记录
	 * @return boolin
	 */
	function removeUser($member_id)
	{
		$tb_Members =& FLEA::getSingleton('Table_Members');
		/*@var $tb_Members Table_Members*/
		
		if(is_array($member_id)){
			
			echo '稍等';
			
		}else {
			$member_data = $tb_Members->find($member_id);
			$user_group_id = $member_data['user_group_id'];
			$isok = $tb_Members->removeByPkv($member_id);
		}
		
		// 更新组成员数量
		$this->updateHas_members($user_group_id);
		return $isok;
	}
	
	/**
	 * 更新一个或者多个用户组的组成员数量，返回成员数量
	 * 没有使用counterCache是为了兼容mysql4
	 * @return int
	 */
	function updateHas_members($user_group_id){
		
		$tb_UserGroups =& FLEA::getSingleton('Table_UserGroups');
		/*@var $tb_UserGroups Table_UserGroups*/
		$tb_Members =& FLEA::getSingleton('Table_Members');
		/*@var $tb_Members Table_Members*/
		
		if(is_array($user_group_id)){
			
			return '未完稍等';
			
		}else{
			$row = array(
				'user_group_id' => $user_group_id,
				'has_members' => $tb_Members->findCount("user_group_id = {$user_group_id}"),
			);
			$tb_UserGroups->update($row);
			return $row['has_members'];
		}
	}
	
	/**
	 * 返回全部角色，不包含关联数据
	 * @return array
	 */
	function rolesList(){
		
		$tb_Roles =& FLEA::getSingleton('Table_Roles');
		/*@var $tb_Roles Table_Roles*/
		
		$tb_Roles->clearLinks();
		return $tb_Roles->findAll();
	}
	
	/**
	 * 返回全部权限
	 * @return array
	 */
	function permissionsList(){
		$tb_Permissions =& FLEA::getSingleton('Table_PermissionGroups');
		/*@var $tb_Permissions Table_PermissionGroups*/
		return $tb_Permissions->findAll();
	}
	
}

?>